package com.example.cookbook.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.cookbook.NetUtil;
import com.example.cookbook.R;
import com.example.cookbook.activity.RecipeDetailActivity;
import com.example.cookbook.bean.RecipeBean;

import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

public class CourseSCAdapter extends RecyclerView.Adapter<CourseSCAdapter.ViewHolder> {
    private Context ncontext;
    private List<RecipeBean> arrlist4;

    public CourseSCAdapter(Context ncontext, List<RecipeBean> arrlist4) {
        this.ncontext = ncontext;
        this.arrlist4 = arrlist4;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int i) {
        View view = LayoutInflater.from(ncontext).inflate(R.layout.recy_sc_item, parent, false);
        ViewHolder nholder = new ViewHolder(view);
        return nholder;
    }

    @Override
    public void onBindViewHolder(final CourseSCAdapter.ViewHolder nholder, int i) {
        final RecipeBean courseInfo = arrlist4.get(i);
        nholder.sc_id.setText(i + 1 + "");
        Glide.with(ncontext).load(courseInfo.getSmallImg()).into(nholder.sc_img);
        nholder.sc_name.setText(courseInfo.getCpName());
        nholder.sc_des.setText(courseInfo.getDes());
        nholder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //数据库里只存储了部分数据，需要使用时查询完整的数据
                NetUtil.queryRecipe_1(courseInfo.getType(), courseInfo.getId(),new NetUtil.OnRecipeQueriedCallback() {
                    @Override
                    public void onSuccess(List<RecipeBean> recipes) {
                        if (recipes.size() < 1) {
                            onFailure(new Exception("data not queried"));
                            return;
                        }
                        //跳转页面的代码
                        Intent intent = new Intent(ncontext, RecipeDetailActivity.class);
                        intent.putExtra("data", recipes.get(0));
                        ncontext.startActivity(intent);
                    }

                    @Override
                    public void onFailure(Exception e) {
                        new Handler(Looper.getMainLooper()).post(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(ncontext, "数据查询失败", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                });
            }
        });
    }

    @Override
    public int getItemCount() {
        return arrlist4.size();
    }

    //创建内部类ViewHolder
    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView sc_id;
        ImageView sc_img;
        TextView sc_name;
        TextView sc_des;

        public ViewHolder(View itemView) {
            super(itemView);
            sc_id = itemView.findViewById(R.id.sc_id);
            sc_img = itemView.findViewById(R.id.sc_img);
            sc_name = itemView.findViewById(R.id.sc_name);
            sc_des = itemView.findViewById(R.id.sc_des);
        }
    }
}
